package 综合应用.T6;

import cn.itcast.xml.XmlUtils;

import java.util.List;

public class T6 {

    public static void main(String[] args) {

        List<Classes> classesList = getClassesList();
        System.out.println(classesList);

        System.out.println("-------------");

        showMaxAvgAgeClassInfo(classesList);

    }

    public static void showMaxAvgAgeClassInfo(List<Classes> classesList){

        int maxAveAge = 0;
        for (Classes classes : classesList) {
            int ageAvg;

            int sumAge = 0;
            for (Student s : classes.getStudentList()) {
                sumAge+=s.getAge();
            }

            ageAvg = sumAge/classes.getStudentList().size();

            if (maxAveAge<ageAvg){
                maxAveAge = ageAvg;
            }
        }
        System.out.println("最大平均年龄是："+maxAveAge);
        System.out.println("最大平均年龄的班级信息如下：");
        for (Classes classes : classesList) {
            int ageAvg;

            int sumAge = 0;
            for (Student s : classes.getStudentList()) {
                sumAge+=s.getAge();
            }

            ageAvg = sumAge/classes.getStudentList().size();

            if (maxAveAge==ageAvg){
                System.out.println(classes.getClassID()+"  "+classes.getClassname());
            }
        }

    }


    public static List<Classes> getClassesList(){

        List<Classes> classesList = XmlUtils.readAll("综合应用/T6/classes.xml", Classes.class);
        List<Student> studentList = XmlUtils.readAll("综合应用/T6/student.xml", Student.class);

        for (Classes c : classesList) {
            for (Student s : studentList) {
                if (c.getClassID().equals(s.getClassID())){
                    c.getStudentList().add(s);
                }
            }
        }
        return classesList;
    }
}